package com.quan.site.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.quan.commons.core.MyBaseServiceImpl;
import com.quan.commons.utils.PageUtils;
import com.quan.site.commons.vo.SiteJoinUsVo;
import com.quan.site.entity.SiteJoinUs;
import com.quan.site.mapper.SiteJoinUsMapper;
import com.quan.site.service.SiteJoinUsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SiteJoinUsServiceImpl extends MyBaseServiceImpl<SiteJoinUsMapper, SiteJoinUs> implements SiteJoinUsService {

    @Autowired
    private SiteJoinUsMapper mapper;

    @Override
    public List<SiteJoinUsVo> queryList(PageUtils pageUtils, SiteJoinUsVo vo) {
        QueryWrapper<SiteJoinUs> queryWrapper = new QueryWrapper<SiteJoinUs>(vo);

        List<SiteJoinUs> list = super.list(pageUtils, queryWrapper);
        List<SiteJoinUsVo> collect = list.stream().map(item -> {
            SiteJoinUsVo bean = new SiteJoinUsVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, SiteJoinUsVo vo) {
        QueryWrapper<SiteJoinUs> queryWrapper = new QueryWrapper<SiteJoinUs>(vo);

        if (StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("name", vo.getKeyword());
            });
        }

        final IPage<SiteJoinUs> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<SiteJoinUs> records = page.getRecords();

        final List<SiteJoinUs> collect = records.stream().map((item) -> {
            SiteJoinUsVo bean = new SiteJoinUsVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }
}